package com.jackchong.utils;

import android.content.Context;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.utils.R;


/**
 * Created by sj9 on 2016/12/2.
 */

public class CommonViewHolder {
    private SparseArray<View> widgets = new SparseArray<>();
    public View convertView;
    public ViewGroup parent;
    public Context context;

    private CommonViewHolder(View convertView, ViewGroup parent) {
        this.convertView = convertView;
        this.parent = parent;
        this.context = parent.getContext();
    }

    public static CommonViewHolder creatCViewHolder(View convertView, @LayoutRes int layout, ViewGroup parent, boolean isAuto) {
        return creatCViewHolder(convertView, View.inflate(parent.getContext(), layout, null), parent, isAuto, true);
    }

    public static CommonViewHolder creatCViewHolder(View convertView, @LayoutRes int layout, ViewGroup parent, boolean isAuto, boolean reuse) {
        return creatCViewHolder(convertView, View.inflate(parent.getContext(), layout, null), parent, isAuto, reuse);
    }

    public static CommonViewHolder creatCViewHolder(View convertView, View item, ViewGroup parent, boolean isAuto, boolean reuse) {
        if (!reuse){
            convertView = item;
            CommonViewHolder holder = new CommonViewHolder(convertView,parent);
            if (isAuto) JAuto.auto(convertView);
            return holder;
        }

        if (convertView == null) {
            convertView = item;
            CommonViewHolder holder = new CommonViewHolder(convertView,parent);
            if (isAuto) JAuto.auto(convertView);
            convertView.setTag(R.id.tag_holder,holder);
        }
        return (CommonViewHolder) convertView.getTag(R.id.tag_holder);
    }

    public void putView(int id, View view) {
        widgets.put(id, view);
    }

    public <T extends View> T getView(int id) {
        if (widgets.get(id) == null) {
            widgets.put(id, convertView.findViewById(id));
        }
        return (T) widgets.get(id);
    }

    public <T extends View> T getView(@IdRes int id, Class<T> clazz) {
        return (T) getView(id);
    }

    public TextView getTV(@IdRes int id) {
        return getView(id, TextView.class);
    }

    public ImageView getIV(@IdRes int id) {
        return getView(id, ImageView.class);
    }

    public void setText(@IdRes int id, String text){
        getTV(id).setText(text);
    }

    public void setTextColor(@IdRes int id, @ColorInt int color){
        getTV(id).setTextColor(color);
    }

    public void setImageResource(@IdRes int id, @DrawableRes int src){
        getIV(id).setImageResource(src);
    }

    public void setImageUrl(@IdRes int id, String url){
        ImageView iv = getIV(id);
        if (TextUtils.isEmpty(url)){
            iv.setImageBitmap(null);
        }else {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.icon_fail)
                    .error(R.drawable.icon_fail);
            Glide.with(iv)
                    .load(url)
                    .apply(options)
                    .into(iv);
        }
    }

    public void setImageUrl(@IdRes int id, String url, @DrawableRes int empty, @DrawableRes int error){
        ImageView iv = getIV(id);
        if (TextUtils.isEmpty(url)){
            iv.setImageBitmap(null);
        }else {

            RequestOptions options = new RequestOptions()
                    .placeholder(empty)
                    .error(error);
            Glide.with(iv)
                    .load(url)
                    .apply(options)
                    .into(iv);
        }
    }

}
